package net.luvina.manageuser.dao.impl;

import java.util.List;

import net.luvina.manageuser.dao.TblDetailUserJapanDao;
import net.luvina.manageuser.hibernate.TblDetailUserJapan;
import net.luvina.manageuser.hibernate.TblUser;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

@Repository("tblDetailUserJapanDao")
public class TblDetailUserJapanDaoImpl extends
		GenericDaoImpl<TblDetailUserJapan, Integer> implements
		TblDetailUserJapanDao {

	public TblDetailUserJapanDaoImpl() {
		super(TblDetailUserJapan.class);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * net.luvina.manageuser.dao.TblDetailUserJapanDao#findTblDetailJapanById
	 * (net.luvina.manageuser.hibernate.TblUser)
	 */
	@SuppressWarnings("unchecked")
	public TblDetailUserJapan findTblDetailJapanById(TblUser tblUser) {
		final int userId = tblUser.getUserId();
		HibernateCallback callback = new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException {

				Query query = session
						.createQuery("from TblDetailUserJapan where userId = "
								+ userId);
				List<TblDetailUserJapan> tblDetailUserJapanList = null;
				try {
					tblDetailUserJapanList = query.list();
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (tblDetailUserJapanList == null
						|| tblDetailUserJapanList.size() == 0) {
					return null;
				} else {
					return tblDetailUserJapanList.get(0);
				}
			}
		};
		return (TblDetailUserJapan) getHibernateTemplate().execute(callback);
	}
}
